//
//  WaveView.m
//  MyProject
//
//  Created by yun on 2018/8/15.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "WaveView.h"

#define kColor [[UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1] colorWithAlphaComponent:0.5]

@interface WaveView ()

@property(nonatomic,assign) CGFloat waveA;// 振幅
@property(nonatomic,assign) CGFloat waveW;// 周期
@property(nonatomic,assign) CGFloat waveP;// 波浪横向偏移
@property(nonatomic,assign) CGFloat waveC;//波浪纵向偏移
@property(nonatomic,assign) CGFloat offset;// 两个波的偏移量
@property(nonatomic,assign) CGFloat radius;// 半径
@property(nonatomic,assign) CGFloat speedY;// 波浪上升或下降的速度
@property(nonatomic,assign) CGFloat speedX;// 横向速度


@property(nonatomic,strong) CADisplayLink *displayLink;

@property(nonatomic,strong) CAShapeLayer *firstLayer;
@property(nonatomic,strong) CAShapeLayer *secondLayer;


@end

@implementation WaveView

/*
 y =Asin（ωx+φ）+ C
 A表示振幅，也就是使用这个变量来调整波浪的高度
 ω表示周期，也就是使用这个变量来调整在屏幕内显示的波浪的数量
 φ表示波浪横向的偏移，也就是使用这个变量来调整波浪的流动
 C表示波浪纵向的位置，也就是使用这个变量来调整波浪在屏幕中竖直的位置。
 */

- (void)dealloc{
    NSLog(@"%s",__func__);
}

- (instancetype)initWithCenter:(CGPoint)center radius:(CGFloat)radius{
    _radius = radius;
    CGFloat x = center.x - radius;
    CGFloat y = center.y - radius;
    return [self initWithFrame:CGRectMake(x, y, 2*radius, 2*radius)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfig];
        [self setup];
    }
    return self;
}

- (void)defaultConfig{
    _waveA = 6;
    _waveW = 0.05;
    _waveP = 0;
    _speedY = 1;
    _speedX = M_PI/30;
    _waveC = 2*_radius;
    _offset = M_PI_2;
}

- (void)setup{
    
    self.layer.cornerRadius = _radius;
    self.layer.masksToBounds = YES;
    
    self.firstLayer = [CAShapeLayer layer];
    self.firstLayer.fillColor = kColor.CGColor;
    [self.layer addSublayer:self.firstLayer];
    
    self.secondLayer = [CAShapeLayer layer];
    self.secondLayer.fillColor = kColor.CGColor;
    [self.layer addSublayer:self.secondLayer];
    
    self.layer.borderWidth = 2;
    self.layer.borderColor = kColor.CGColor;
    [self startLink];
}

- (void)startLink{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(waveMove:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:(NSRunLoopCommonModes)];
}

- (void)stopLink{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)waveMove:(CADisplayLink *)link{
    /**
     * CoreGraphics实现(UIBezierPath也可以)
     */
    
    _waveP += _speedX;
    CGMutablePathRef firstPath = CGPathCreateMutable();// 可变路径1
    CGMutablePathRef secondPath = CGPathCreateMutable();// 可变路径2
    CGFloat y1 = 2*_radius + _waveA/2.f;// y轴
    CGFloat y2 = 2*_radius + _waveA/2.f;// y轴
    
    CGPathMoveToPoint(firstPath, NULL, 0, 2*_radius);
    CGPathAddLineToPoint(firstPath, NULL, 0, y1);
    
    CGPathMoveToPoint(secondPath, NULL, 0, 2*_radius);
    CGPathAddLineToPoint(secondPath, NULL, 0, y2);
    
    if (2*_radius*(1-_percent) < _waveC) {// _waveC低于目标值,即上升（因为坐标系反的，所以这样说）
        _waveC -= _speedY;
        if (_waveC < 2*_radius*(1-_percent)) {
            _waveC = 2*_radius*(1-_percent);
        }
    } else if (2*_radius*(1-_percent) > _waveC){// _waveC高于目标值,即下降
        _waveC += _speedY;
        if (_waveC > 2*_radius*(1-_percent)) {
            _waveC = 2*_radius*(1-_percent);
        }
    } else {
        _waveC = 2*_radius*(1-_percent);
    }
    
    for (int i = 0; i <= ceilf(2*_radius); i++) {
        y1 = _waveA*sin(_waveW * i + _waveP) + _waveC;
        CGPathAddLineToPoint(firstPath, NULL, i, y1);
        
        y2 = _waveA*sin(_waveW * i + _waveP + _offset) + _waveC;
        CGPathAddLineToPoint(secondPath, NULL, i, y2);
    }
    CGPathAddLineToPoint(firstPath, NULL, ceilf(2*_radius), 2*_radius);
    CGPathCloseSubpath(firstPath);
    self.firstLayer.path = firstPath;
    CGPathRelease(firstPath);
    
    CGPathAddLineToPoint(secondPath, NULL, ceilf(2*_radius), 2*_radius);
    CGPathCloseSubpath(secondPath);
    self.secondLayer.path = secondPath;
    CGPathRelease(secondPath);
    
}

- (void)setPercent:(CGFloat)percent{
    
    if (percent < 0) {
        _percent = 0;
    } else if (percent > 1){
        _percent = 1;
    } else {
        _percent = percent;
    }
    
}


@end
